//
//  PersonTicketViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-27.
//
//

#import "PersonTicketViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "NetworkAlertView.h"
#import "UPPayPlugin.h"

@interface PersonTicketViewController ()

@end

@implementation PersonTicketViewController
{
    UIImageView *_imageview;
    UILabel *_airInfoLbl;
    UILabel *_dateLbl;
    UILabel *_timeLbl;
    
    UIImageView *_listview;
    UILabel *_confirmLbl;
    UILabel *_priceLbl;
    UILabel *_portpriceLbl;
    UILabel *_oilpriceLbl;
    UILabel *_portnameLbl;
    UILabel *_portphoneLbl;
    UILabel *_contactnameLbl;
    UILabel *_contactphoneLbl;
}

- (void)dealloc
{
    [_imageview release];
    [_confirmLbl release];
    [_airInfoLbl release];
    [_dateLbl release];
    [_timeLbl release];
    [_listview release];
    [_priceLbl release];
    [_portpriceLbl release];
    [_oilpriceLbl release];
    [_portnameLbl release];
    [_portphoneLbl release];
    [_contactnameLbl release];
    [_contactphoneLbl release];
    
    _imageview = nil;
    _confirmLbl = nil;
    _airInfoLbl = nil;
    _dateLbl = nil;
    _timeLbl = nil;
    _listview = nil;
    _priceLbl = nil;
    _portpriceLbl = nil;
    _oilpriceLbl = nil;
    _portnameLbl = nil;
    _portphoneLbl = nil;
    _contactnameLbl = nil;
    _contactphoneLbl = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化视图
- (void)init_view
{
    self.title = @"机票";
    //加载背影
     [self.view setBackgroundColor:BackgroundColor];
    
    
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    UIScrollView *myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = [UIColor whiteColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(width, 1000 + [_userArray count]*100);
    [myscrollview setContentSize:newSize];
    
    //初始化机票信息
    [self initTicketView:myscrollview];
    
    //初始化确认信息
    [self initConfirmView:myscrollview];
    
    [self.view addSubview:myscrollview];
}

//初始化机票信息
- (void) initTicketView:(UIScrollView*)myscrollview
{
    UILabel *confirmLbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 120)];
    confirmLbl.backgroundColor = [UIColor whiteColor];
    [myscrollview addSubview:confirmLbl];
    
    //加载图片
    _imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rili_03"]];
    _imageview.frame = CGRectMake(20, 20, 90, 100);
    [myscrollview addSubview:_imageview];
    
    //加载日期
    _dateLbl = [[UILabel alloc]initWithFrame:CGRectMake(35, 30, 85, 30)];
    _dateLbl.font = [UIFont systemFontOfSize:16];
    _dateLbl.textColor = [UIColor whiteColor];
    _dateLbl.backgroundColor = [UIColor clearColor];
    _dateLbl.text = [self.startDate substringToIndex:7];
    [myscrollview addSubview:_dateLbl];
    
    //加载时间
    _timeLbl = [[UILabel alloc]initWithFrame:CGRectMake(43, 52, 70, 70)];
    _timeLbl.font = [UIFont systemFontOfSize:50];
    _timeLbl.textColor = [UIColor whiteColor];
    _timeLbl.backgroundColor = [UIColor clearColor];
    _timeLbl.text = [self.startDate substringFromIndex:8];
    [myscrollview addSubview:_timeLbl];
    
    //加载详情
    NSString *lineStr = [NSString stringWithFormat:@"%@ --> %@", self.startCity, self.arriveCity];
    NSString *airinfoStr = [NSString stringWithFormat:@"航班  %@", self.orderInfo.fltno];
    NSString *startInfoStr = [NSString stringWithFormat:@"起飞  %@", self.orderInfo.scAirdrome];
    NSString *arriveInfoStr = [NSString stringWithFormat:@"到达  %@", self.orderInfo.ecAirdrome];
    NSString *seatInfoStr = [NSString stringWithFormat:@"舱位  %@", self.orderInfo.classname];
    
    _airInfoLbl = [[UILabel alloc]initWithFrame:CGRectMake(130, 10, 170, 120)];
    _airInfoLbl.lineBreakMode = UILineBreakModeWordWrap;
    _airInfoLbl.numberOfLines = 0;
    _airInfoLbl.font = [UIFont systemFontOfSize:13];
    _airInfoLbl.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@", lineStr, airinfoStr, startInfoStr, arriveInfoStr, seatInfoStr];
    [myscrollview addSubview:_airInfoLbl];
}

//初始化确认信息
- (void) initConfirmView:(UIScrollView*)myscrollview
{
    //加载图片
    _listview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"表格7行.png"]];
    _listview.frame = CGRectMake(10, 140, 300, 250);
    [myscrollview addSubview:_listview];
    
    NSMutableString *namelist = [[NSMutableString alloc] init];
    for (int i = 0; i < [self.userArray count]; i++) {
        NSDictionary *useDict = [self.userArray objectAtIndex:i];
        [namelist appendFormat:[NSString stringWithFormat:@" %@", [useDict objectForKey:@"name"]]];
    }
    NSDictionary *tempUseDict = [self.userArray objectAtIndex:0];
    NSString *phonelist = [NSString stringWithFormat:@"%@:%@", [tempUseDict objectForKey:@"contactname"],
                           [tempUseDict objectForKey:@"contactphone"]];
    //确认信息
    NSArray *stringArray = [[NSArray arrayWithObjects:[NSString stringWithFormat:@"起飞时间：%@", self.orderInfo.deptime],
                             [NSString stringWithFormat:@"到达时间：%@", self.orderInfo.arrtime],
                             [NSString stringWithFormat:@"单价：￥%@", self.orderInfo.saleprice],
                             [NSString stringWithFormat:@"机建：￥%@", self.orderInfo.buildfee],
                             [NSString stringWithFormat:@"燃油：￥%@", self.orderInfo.fuelfee],
                             [NSString stringWithFormat:@"乘机人：%@", namelist],
                             [NSString stringWithFormat:@"联系人：%@", phonelist], nil] retain];
    int i;
    for (i = 0; i < [stringArray count]; i++) {
        _confirmLbl = [[UILabel alloc]initWithFrame:CGRectMake(40, 190 + i*27, 240, 25)];
        _confirmLbl.font = [UIFont systemFontOfSize:15];
        _confirmLbl.backgroundColor = [UIColor clearColor];
        _confirmLbl.text = [stringArray objectAtIndex:i];
        [myscrollview addSubview:_confirmLbl];
    }
    [namelist release];
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 225 + i*27, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStep setTitle:@"确认支付" forState:UIControlStateNormal];
    nextStep.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    nextStep.layer.borderColor = [UIColor grayColor].CGColor;
    nextStep.layer.borderWidth = 1 ;
    nextStep.layer.cornerRadius = 10 ;
    [myscrollview addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge) forControlEvents:UIControlEventTouchUpInside];
}

//确认支付
- (void)confirmMessge
{
    [[NetworkAlertView shareView] showNetView];
    AirplaneManager *plainmanager = [AirplaneManager shareManager];
    plainmanager.delegate = self;
    [plainmanager buyAirTicket:self.orderInfo
                     userArray:self.userArray
                     startData:self.startDate];

}

#pragma networking
//购买机票
- (void)buyAirTicket:(BOOL)isSuccess
                  tn:(NSString*)tn
{
    [[NetworkAlertView shareView] delView];
    if (isSuccess) {
        [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:PAY_MODE_REALEASE viewController:self delegate:self];
    } else {
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"下单失败" message:nil delegate:self cancelButtonTitle:@"下单失败，请重新尝试，谢谢" otherButtonTitles: nil] autorelease];
        [baseAlert show];
    }
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

@end
